package com.xuxin.baby.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.ReaderVideoAdapter;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.ReaderVideoBean;
import com.xuxin.baby.contract.ReaderVideoContract;
import com.xuxin.baby.presenter.ReaderVideoPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.27 0027
 * 视频文章
 */
public class ReaderVideoActivity extends BaseActivity implements ReaderVideoContract.View {
    private final ReaderVideoContract.Presenter presenter = new ReaderVideoPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;

    private ReaderVideoAdapter myAdapter;
    private String tenantId;//租户id
    private String token;//授权token
    private int pageNum = 0;
    private String categoryId;//分类id
    private String categoryType;//分类类型
    private String keywords;//关键词
    private String ageRge;//年龄段

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_smart);
    }

    @Override
    public void initView() {
        String title = getIntent().getStringExtra("title");
        categoryId = getIntent().getStringExtra("categoryId");
        categoryType = getIntent().getStringExtra("categoryType");
        ageRge = getIntent().getStringExtra("ageRge");
        keywords = getIntent().getStringExtra("keywords");
        if (!StringUtils.isEmpty(keywords)) {
            title_name.setText(keywords);
        } else {
            title_name.setText(title);
        }
        smart_refresh.setEnableLoadMore(false);
        smart_refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onRefresh");
                pageNum = 0;
                myAdapter.setList(null);
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onLoadMore");
                handler.sendEmptyMessage(0);
            }
        });
        RecycleUtils.init(smart_recycle);
        myAdapter = new ReaderVideoAdapter();
        smart_recycle.setAdapter(myAdapter);
        View head = View.inflate(context, R.layout.view_search_head, null);
        EditText search = head.findViewById(R.id.search);
        search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView text, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    keywords = text.getText().toString();
                    if (!StringUtils.isEmpty(keywords)) {
                        smart_refresh.autoRefresh();
                    }
                }
                return false;
            }
        });
        if (StringUtils.isEmpty(keywords)) {
            myAdapter.addHeaderView(head);
        }
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString(BaseConstant.videoId, myAdapter.getData().get(position).getId());
                ActivityUtils.startActivity(bundle, VideoActivity.class);
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        smart_refresh.autoRefresh();
    }

    @Override
    public void onComplete() {
        smart_refresh.finishRefresh();
        smart_refresh.finishLoadMore();
    }

    @Override
    @OnClick({R.id.title_left})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getReaderVideo(tenantId, token, categoryId, keywords, null, categoryType, ageRge, pageNum);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(ReaderVideoBean readerVideoBean) {
        myAdapter.addData(readerVideoBean.getResult().getItems());
        if (readerVideoBean.getResult().getTotalCount() > myAdapter.getData().size()) {
            pageNum++;
            smart_refresh.setEnableLoadMore(true);
        } else {
            smart_refresh.setEnableLoadMore(false);
        }
    }
}